<template>
  <el-dialog title="注册" :close-on-click-modal="false" append-to-body :visible.sync="visible" class="AJ-dialog" lock-scroll width="500px" :show-close="false" @closed="dialogClose">
    <el-form ref="form" :model="form" label-align="top" :rules="rules">
      <el-form-item label="账号" prop="account">
        <el-input placeholder="请输入账号" v-model="form.account" clearable></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input placeholder="请输入密码" type="password" v-model="form.password" clearable></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkword">
        <el-input placeholder="请输入确认密码密码" type="password" v-model="form.checkword" clearable></el-input>
      </el-form-item>
    </el-form>

    <span slot="title" class="dialog-footer">
      <div style="display: flex; justify-content: space-between; align-items: center">
        <div>注册</div>
        <div>
          <el-button type="primary" @click="register()" :loading="registerLoading" size="small">注 册</el-button>
          <el-button @click="visible = false" size="small">关 闭</el-button>
        </div>
      </div>
    </span>
  </el-dialog>
</template>
<script>
import request from '@/utils/request';
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      visible: false,
      registerLoading: false,
      form: {
        account: '',
        password: '',
        checkword: ''
      },
      rules: {
        account: [
          { required: true, message: '请输入注册账号', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]+$/, message: '注册账号仅允许大小写字母和数字', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入注册密码', trigger: 'blur' }],
        checkword: [{ validator: validatePass, trigger: 'blur' }]
      }
    };
  },
  methods: {
    initData() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs.form.resetFields();
      });
    },
    register() {
      this.registerLoading = true;
      request({
        url: '/api/web/moralSystem/backScore',
        method: 'post',
        data: this.form
      })
        .then(res => {
          this.$message({
            message: '注册成功',
            type: 'success'
          });
          this.visible = false;
          this.registerLoading = false;
        })
        .catch(e => {
          this.registerLoading = false;
        });
    },
    dialogClose() {
      Object.assign(this.$data, this.$options.data());
    }
  }
};
</script>
<style lang="less" scoped></style>
